html='''
<div class=wrap">
<div id="container">
<ul class="list">
<li class = "item-0">first item</li>
<li class = "item-1"><a href = "link2.html">second item</a></li>
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
<li class = "item-1" active><a href = "link4.html">fourth item</a></li>
<li class = "item-0"><a href = "link5.html">fif item</a>
</ul>
</div>
</div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.remove_class('active')
print(li)
li.add_class('active')
print(li)
print('***************************')
html = '''
<ul class="list">
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
</ul>
'''
doc = pq(html)
li = doc('.item-0.active')
print(li)
li.attr('name','link')#第一个参数是属性，第二个参数是属性值
print(li)#li多了一个原本不存在的属性name，
li.text('changed item')
print(li)#li内部的文本全被改为传入的字符串文本了
li.html('<span>changed item</span>')
print(li)#调用html（）方法传入HTML文本后，li节点内部又变为传入的HTML文本了
# 如果attr（）方法只传入第一个参数的属性名，则是获取这个属性值，如果传入第二个参数，可以用来修改属性值。
# text（）和html（）方法如果不传入参数，则是获取节点内部纯文本和HTML文本，如果传入参数，则进行赋值


html = '''
<div class="wrap">
    Hello,World
<p>This is a paragraph.</p>
</div>
'''
doc = pq(html)
wrap = doc('.wrap')
print(wrap.html())
print(wrap.text())
print('++++++++++')
wrap.find('p').remove()
print(wrap.text())


# 伪类选择器
html='''
<div class=wrap">
<div id="container">
<ul class="list">
<li class = "item-0">first item</li>
<li class = "item-1"><a href = "link2.html">second item</a></li>
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
<li class = "item-1" active><a href = "link4.html">fourth item</a></li>
<li class = "item-0"><a href = "link5.html">fif item</a>
</ul>
</div>
</div>
'''
doc = pq(html)
li = doc('li:first-child')
print(li)
li = doc('li:last-child')
print(li)
li = doc('li:nth-child(2)')
print(li)
li = doc('li:gt(2)')

print(li)
li =doc('li:nth-child(2n)')
print(li)
li = doc('li:contains(second)')
print(li)